# JavaScript Numbers

« Previous
Next Chapter »

JavaScript has only one type of number.

Numbers can be written with, or without, decimals.

## JavaScript Numbers

JavaScript numbers can be written with, or without decimals:

## Example

var x = 34.00; // A number with decimals
var y = 34; // A number without decimals

Extra large or extra small numbers can be written with scientific (exponent) notation:

## Example

var x = 123e5; // 12300000
var y = 123e-5; // 0.00123

## JavaScript Numbers are Always 64-bit Floating Point

Unlike many other programming languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point etc.

JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard.
This format stores numbers in 64 bits, where the number (the fraction) is stored in bits 0 to 51, the exponent in bits 52 to 62, and the sign in bit 63:

Value (aka Fraction/Mantissa) | Exponent | Sign
--- | --- | ---
52 bits (0 - 51) | 11 bits (52 - 62) | 1 bit (63)

## Precision

Integers (numbers without a period or exponent notation) are considered accurate up to 15 digits.

## Example

var x = 999999999999999; // x will be 999999999999999
var y = 9999999999999999; // y will be 10000000000000000
Try it yourself »

The maximum number of decimals is 17, but floating point arithmetic is not always 100% accurate:

## Example

var x = 0.2 + 0.1; // x will be 0.30000000000000004
Try it yourself »

To solve the problem above, it helps to multiply and divide:

## Example

var x = (0.2 * 10 + 0.1 * 10) / 10; // x will be 0.3
Try it yourself »

## Hexadecimal

JavaScript interprets numeric constants as hexadecimal if they are preceded by 0x.

## Example

var x = 0xFF; // x will be 255
Try it Yourself »

| Never write a number with a leading zero (like 07). Some JavaScript versions interpret numbers as octal if they are written with a leading zero.

By default, Javascript displays numbers as base 10 decimals.

But you can use the toString() method to output numbers as base 16 (hex), base 8 (octal), or base 2 (binary).

## Example

var myNumber = 128;
myNumber.toString(16); // returns 80
myNumber.toString(8); // returns 200
myNumber.toString(2); // returns 10000000
Try it Yourself »

## Infinity

Infinity (or -Infinity) is the value JavaScript will return if you calculate a number outside the largest possible number.

## Example

var myNumber = 2;
while (myNumber != Infinity) { // Execute until Infinity
myNumber = myNumber * myNumber;
}
Try it yourself »

Division by 0 (zero) also generates Infinity:

## Example

var x = 2 / 0; // x will be Infinity
var y = -2 / 0; // y will be -Infinity
Try it Yourself »

Infinity is a number: typeOf Infinity returns number.

## Example

typeof Infinity; // returns "number"
Try it Yourself »

## NaN - Not a Number

NaN is a JavaScript reserved word indicating that a value is not a number.

Trying to do arithmetic with a non-numeric string will result in NaN (Not a Number):

## Example

var x = 100 / "Apple"; // x will be NaN (Not a Number)
Try it yourself »

However, if the string contains a numeric value , the result will be a number:

## Example

var x = 100 / "10"; // x will be 10
Try it yourself »

You can use the global JavaScript function isNaN() to find out if a value is a number.

## Example

var x = 100 / "Apple";
isNaN(x); // returns true because x is Not a Number
Try it Yourself »

Watch out for NaN. If you use NaN in a mathematical operation, the result will also be NaN.

## Example

var x = NaN;
var y = 5;
var z = x + y; // z will be NaN
Try it Yourself »

NaN is a number: typeOf NaN returns number.

## Example

typeof NaN; // returns "number"
Try it Yourself »

## Numbers Can be Objects

Normally JavaScript numbers are primitive values created from literals: var x = 123

But numbers can also be defined as objects with the keyword new: var y = new Number(123)

## Example

var x = 123;
var y = new Number(123);
typeof x; // returns number
typeof y; // returns object
Try it yourself »

| Don't create Number objects. They slow down execution speed, and produce nasty side effects:

## Example

var x = 123;
var y = new Number(123);
(x === y) // is false because x is a number and y is an object.
Try it yourself »

## Number Properties and Methods

Primitive values (like 3.14 or 2014), cannot have properties and methods (because they are not objects).

But with JavaScript, methods and properties are also available to primitive values, because JavaScript treats primitive values as objects when executing methods and properties.

## Number Properties

Property | Description
--- | ---
MAX_VALUE | Returns the largest number possible in JavaScript
MIN_VALUE | Returns the smallest number possible in JavaScript
NEGATIVE_INFINITY | Represents negative infinity (returned on overflow)
NaN | Represents a "Not-a-Number" value
POSITIVE_INFINITY | Represents infinity (returned on overflow)

## Example

var x = Number.MAX_VALUE;
Try it yourself »

Number properties belongs to the JavaScript's number object wrapper called Number.

These properties can only be accessed as Number.MAX_VALUE.

Using myNumber.MAX_VALUE, where myNumber is a variable, expression, or value, will return undefined:

## Example

var x = 6;
var y = x.MAX_VALUE; // y becomes undefined
Try it yourself »

| Number methods are covered in the next chapter

« Previous
Next Chapter »

### WEB HOSTING

UK Reseller Hosting

### WEB BUILDING

FREE Website BUILDER Free HTML5 Templates

### W3SCHOOLS EXAMS

HTML, CSS, JavaScript, PHP, jQuery, XML, and ASP Certifications

### SHARE THIS PAGE

### Color Picker

### HTML/CSS

HTML Tutorial HTML5 Tutorial CSS Tutorial CSS3 Tutorial Bootstrap Tutorial

### JavaScript

JavaScript Tutorial jQuery Tutorial jQuery Mobile Tutorial AngularJS Tutorial AJAX Tutorial JSON Tutorial

### Graphics

Canvas Tutorial SVG Tutorial Google Maps Tutorial

### Server Side

SQL Tutorial PHP Tutorial ASP Tutorial ASP.NET Tutorial VBScript Tutorial AppML Tutorial

### Web

Web Building Web Statistics Web Certification

### XML

XML Tutorial DTD Tutorial Schema Tutorial XML DOM Tutorial XPath Tutorial XSLT Tutorial XQuery Tutorial XSL-FO Tutorial RSS Tutorial WSDL Tutorial

### HTML/CSS

HTML Tag Reference HTML Event Reference HTML Color Reference CSS 1,2,3 Reference Bootstrap Reference

### JavaScript

JavaScript Reference HTML DOM Reference jQuery Reference jQuery Mobile Reference Google Maps Reference

### Server Side

PHP Reference SQL Reference ASP Reference VBScript Reference Razor Reference ASP.NET Reference

### XML

XML DOM Reference XSLT Reference XSL-FO Reference XPath Reference XQuery Reference Schema Reference RSS Reference SVG Reference

### HTML Charsets

HTML Character Sets HTML ASCII HTML ANSI HTML Windows-1252 HTML ISO-8859-1 HTML Symbols HTML UTF-8

### HTML/CSS

HTML Examples CSS Examples

### Browser Scripting

JavaScript Examples HTML DOM Examples jQuery Examples jQuery Mobile Examples AngularJS Examples AJAX Examples

### Server Scripting

PHP Examples ASP Examples VBScript Examples Razor Examples .NET Examples

### XML Languages

XML Examples DTD Examples XML DOM Examples XSL Examples XSLT Examples XPath Examples XQuery Examples Schema Examples RSS Examples SVG Examples